package com.hebeu.card.sys.action.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hebeu.card.sys.entity.user.User;
import com.hebeu.card.sys.service.impl.user.UserServiceImpl;
import com.hebeu.core.util.CURDAction;

//         http://localhost:8080/SchoolCard/sys/user/list.action
@Namespace(value = "/sys/user")
@Results({
		@Result(name = "save", location = "list.action", type = "redirect"),
		@Result(name = "input", location = "/WEB-INF/content/sys/user/user.jsp"),
		@Result(name = "list", location = "/WEB-INF/content/sys/user/user-list.jsp"),
		@Result(name = "delete", location = "list.action", type = "redirect"),
		@Result(name = "detail", location = "/test.jsp"),
		@Result(name = "search", location = "/test.jsp") })
public class UserAction extends CURDAction<User> {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private User entity;
	private UserServiceImpl udi = new UserServiceImpl();
	private int id;
	private List<User> userList = new ArrayList<User>();
	private int[] checkedIds;

	@Override
	@Action(value = "list")
	public String list() {
		userList = udi.getAll();
		return "list";
	}

	@Override
	@Action(value = "delete")
	public String delete() {
		for (int i : checkedIds) {
			udi.delete(i);
		}
		return "delete";
	}

	@Override
	@Action(value = "save")
	public String save() {
		if(entity.getPass() == null) {
			entity.setPass("000000");
		}
		udi.save(entity);
		return "save";
	}

	@Override
	@Action(value = "detail")
	public String detail() {
		entity = udi.get(id);
		return "detail";
	}

	@Override
	@Action(value = "search")
	public String search() {
		return "search";
	}

	@Override
	@Action(value = "input")
	public String input() {
		return "input";
	}

	// //////////////////////////////////////////////////////////////////////////////////////////
	public User getEntity() {
		return entity;
	}

	public void setEntity(User entity) {
		this.entity = entity;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public int[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(int[] checkedIds) {
		this.checkedIds = checkedIds;
	}

	@Override
	public User getModel() {
		if (entity == null) {
			entity = new User();
		}
		return entity;
	}

}
